<?php
/* SVN FILE: $Id: create.php 255 2009-12-22 06:45:47Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Bank - Create accounts
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 255 $
* @lastrevision $Date: 2009-12-22 06:45:47 +0000 (Tue, 22 Dec 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-12-22 06:45:47 +0000 (Tue, 22 Dec 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/plugins/bank/publicAction/create.php $
*/

class plugin_public_bank_create extends marketPublicCommand
{
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		if($this->request['request_method'] == 'post')
		{
			if(!marketRegistry::hasPluginPermission('use'))
			{
				$this->registry->getClass('output')->showError('no_permission');
			}
			
			if(!isset($this->caches['ibm_bank_starter'][$this->request['starter_id']]))
			{
				$this->registry->getClass('output')->showError('no_permission');
			}
			
			$this->registry->getClass('class_localization')->loadLanguageFile( array( 'bank_bank' ) );
			
			$fee = numbers::round(numbers::multiply($this->caches['ibm_bank_starter'][$this->request['starter_id']]['s_interest_time'],$this->memberData['market_deduct']));
			if(numbers::compare($fee, $this->memberData[$this->settings['market_members_field']]) > 0)
			{
				$this->registry->getClass('output')->showError('not_enough_money');
			}
			
			if(!$this->request['accountName'])
			{
				$this->registry->getClass('output')->showError('must_need_account_name');
			}
			
			$settings = unserialize($this->caches['market_plugin'][$this->request['plugin']]['plugin_config']);
			if($settings['max_account'])
			{
				$count = $this->DB->buildAndFetch(array('select'=>'COUNT(*) as total','from'=>'market_bank_accounts','where'=>"a_mem_id=".$this->memberData['member_id']));
				
				if($count['total'] >= $settings['max_account'])
				{
					$this->registry->getClass('output')->showError('too_many_accounts');
				}
			}
			
			$account = array();
			
			$account['a_random'] = numbers::multiply(1000000,(float)microtime());
			$account['a_mem_id'] = $this->memberData['member_id'];
			$account['a_name'] = $this->request['accountName'];
			$account['a_open'] = IPS_UNIX_TIME_NOW;
			$account['balance'] = 0;
			$account['a_interest_time'] = $this->caches['ibm_bank_starter'][$this->request['starter_id']]['s_interest_time'];
			$account['a_last_interest'] = IPS_UNIX_TIME_NOW - IPS_UNIX_TIME_NOW % 60;
			$account['interest_rate'] = $this->caches['ibm_bank_starter'][$this->request['starter_id']]['s_interest_rate'];
			$account['a_deposit_fee'] = $this->caches['ibm_bank_starter'][$this->request['starter_id']]['s_deposit_fee'];
			$account['a_withdraw_fee'] = $this->caches['ibm_bank_starter'][$this->request['starter_id']]['s_withdraw_fee'];
			$account['a_delete_fee'] = numbers::round(numbers::multiply($this->caches['ibm_bank_starter'][$this->request['starter_id']]['delete_fee'],$this->memberData['market_deduct']));
			
			$this->DB->insert('market_bank_accounts',$account);
			$account['account_id'] = $this->DB->getInsertId();
			$this->DB->update('members',array(
												$this->settings['market_members_field'] => numbers::subtract($this->memberData[$this->settings['market_members_field']], $fee),
			),"member_id=".$this->memberData['member_id']);
			
			bankPluginFunctions::logTransaction(bankPluginFunctions::getBankAccountID($account),'create',0,0,$fee);
			
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['account_created'], $this->settings['base_url_with_app'] . "plugin=".$this->request['plugin'] . "&amp;action=manage&amp;account_id=" . $account['account_id'] );
		}
		else
		{
			if(!marketRegistry::hasPluginPermission('view'))
			{
				$this->registry->getClass('output')->showError('no_permission');
			}
			
			$this->registry->getClass('class_localization')->loadLanguageFile( array( 'bank_bank' ) );
			
			$this->request['account_id'] = intval($this->request['account_id']);
			
			$content = $this->registry->getClass('output')->getTemplate('ibm_plugin_bank')->listBankAccoutStarters($this->caches['ibm_bank_starter']);
			
			$template = $this->registry->getClass('output')->getTemplate('ibmarket')->ibmarketHTMLWrapper($content);
			
			$this->registry->getClass('output')->setTitle( $this->settings['market_name'] );
			$this->registry->getClass('output')->addContent( $template );
			
			$this->registry->getClass('output')->sendOutput();
		}
	}
}